package com.example.sports_meet.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.sports_meet.ManagerActivity;
import com.example.sports_meet.R;
import com.example.sports_meet.adapter.OptionAdapter;
import com.example.sports_meet.athlete.AthleteActivity;
import com.example.sports_meet.entity.Option;
import com.example.sports_meet.volunteer.VolunteerActivity;

import java.util.ArrayList;
import java.util.List;


public class OptionFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_option, container, false);
        ListView lv_choose = root.findViewById(R.id.lv_choose);
        List<Option> options = new ArrayList<>();
        options.add(new Option("志愿者", VolunteerActivity.class));
        options.add(new Option("运动员", AthleteActivity.class));
        options.add(new Option("管理员", ManagerActivity.class));
        OptionAdapter adapter = new OptionAdapter(getContext(), options);
        lv_choose.setAdapter(adapter);
        lv_choose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        // 点击第一个选项，跳转到Activity1
                        startActivity(new Intent(getActivity(), VolunteerActivity.class));
                        break;
                    case 1:
                        // 点击第二个选项，跳转到Activity2
                        startActivity(new Intent(getActivity(), AthleteActivity.class));
                        break;
                    case 2:
                        // 点击第三个选项，弹出输入框,输入正确跳转到Activity3
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setTitle("输入身份码");

                        final EditText input = new EditText(getActivity());
                        input.setInputType(InputType.TYPE_CLASS_NUMBER);
                        builder.setView(input);
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String userInput = input.getText().toString();
                                if (userInput.equals("345")) {
                                    //输入正确.跳转到Activity3
                                    startActivity(new Intent(getActivity(), ManagerActivity.class));
                                } else {
                                    //输入错误,显示错误信息
                                    Toast.makeText(getContext(), "输入错误,请重试!", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                        builder.show();

                        break;

                }
            }
        });

        return root;
    }

}